package com.devbobcorn.nekoration.client.rendering.blockentities;

import com.devbobcorn.nekoration.blocks.ItemDisplayBlock;
import com.devbobcorn.nekoration.blocks.entities.ItemDisplayBlockEntity;
import com.mojang.blaze3d.matrix.MatrixStack;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.model.ItemCameraTransforms;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.util.math.vector.Vector3f;

public class CupboardRenderer extends TileEntityRenderer<ItemDisplayBlockEntity> {
	public CupboardRenderer(TileEntityRendererDispatcher tileEntityRendererDispatcher) {
		super(tileEntityRendererDispatcher);
	}

	@Override
	public void render(ItemDisplayBlockEntity tileEntity, float partialTicks, MatrixStack stack, IRenderTypeBuffer buffers, int combinedLight, int combinedOverlay) {
        stack.pushPose();
        if (tileEntity.wallShelf)
            renderShelfItems(tileEntity, stack, buffers, combinedLight);
        else renderCabinetItems(tileEntity, stack, buffers, combinedLight);
        stack.popPose();
	}

    private void renderShelfItems(ItemDisplayBlockEntity tileEntity, MatrixStack stack, IRenderTypeBuffer buffers, int combinedLight){
        // Items...
        stack.translate(0.5D, 0.5D, 0.5D);
        stack.mulPose(Vector3f.YP.rotationDegrees(-tileEntity.getBlockState().getValue(ItemDisplayBlock.FACING).get2DDataValue() * 90.0F));
        float sc = 0.5F;
        stack.scale(sc, sc, sc);
        stack.mulPose(Vector3f.XP.rotationDegrees(-10));

        // 0 1 2 3
        stack.translate(-1.35D, 0.2D, -0.5D);
        for (int i = 0;i < 4;i++){
            stack.translate(0.55D, 0.0D, -0.0D);
            Minecraft.getInstance().getItemRenderer().renderStatic(tileEntity.renderItems[i], ItemCameraTransforms.TransformType.GROUND,
                combinedLight, OverlayTexture.NO_OVERLAY, stack, buffers);
        }
    }

    private void renderCabinetItems(ItemDisplayBlockEntity tileEntity, MatrixStack stack, IRenderTypeBuffer buffers, int combinedLight){
        // Items...
        stack.translate(0.5D, 0.5D, 0.5D);
        stack.mulPose(Vector3f.YP.rotationDegrees(-tileEntity.getBlockState().getValue(ItemDisplayBlock.FACING).get2DDataValue() * 90.0F));
        float sc = 0.5F;
        stack.scale(sc, sc, sc);
        stack.mulPose(Vector3f.XP.rotationDegrees(-10));

        // 0 1
        // 2 3
        stack.translate(-0.4D, 0.2D, -0.5D);
        Minecraft.getInstance().getItemRenderer().renderStatic(tileEntity.renderItems[0], ItemCameraTransforms.TransformType.GROUND,
            combinedLight, OverlayTexture.NO_OVERLAY, stack, buffers);

        stack.translate(0.8D, 0.0D, 0.0D);
        Minecraft.getInstance().getItemRenderer().renderStatic(tileEntity.renderItems[1], ItemCameraTransforms.TransformType.GROUND,
            combinedLight, OverlayTexture.NO_OVERLAY, stack, buffers);

        stack.translate(0.0D, -0.7D, -0.2D);
        Minecraft.getInstance().getItemRenderer().renderStatic(tileEntity.renderItems[3], ItemCameraTransforms.TransformType.GROUND,
            combinedLight, OverlayTexture.NO_OVERLAY, stack, buffers);

        stack.translate(-0.8D, 0.0D, 0.0D);
        Minecraft.getInstance().getItemRenderer().renderStatic(tileEntity.renderItems[2], ItemCameraTransforms.TransformType.GROUND,
            combinedLight, OverlayTexture.NO_OVERLAY, stack, buffers);
    }

	@Override
	public boolean shouldRenderOffScreen(ItemDisplayBlockEntity BlockEntity) {
		return false;
	}
}
